package study.thread.CriticalSection;

/**
 * @author Administrator
 * It is possible to show the critical section problem, but not absolutely. 
 * Because computer has good enough resources or CPU today.
 */
public class ProblemThread extends Thread {
	final static int NUM_THREADS = 2000, COUNT = 1000;
	static int value =0;
	
	private static void increment(){
		value++;
	}
	
	public void run(){
		for (int i=0; i<COUNT; i++){
			increment();
			try {
				Thread.sleep(1);
				
			}catch (InterruptedException ie){
				
			}
		}
	}
	
	
	/**
	 *main 
	 *@param args
	 */
	public static void main(String[] args) {
		Thread[] myThread;
		myThread = new ProblemThread[NUM_THREADS];
		for (int i=0; i<NUM_THREADS;i++){
			myThread[i]= new ProblemThread();
			myThread[i].start();
		}
		
		try {
			for (int i=0; i<NUM_THREADS; i++){
				myThread[i].join();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("\n The actual value is " + value + 
				". The correct value is "+ (NUM_THREADS * COUNT));
	}

}
